package com.escom.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.escom.R;
import com.escom.dao.MedicoDAO;
import com.escom.service.MedicoWebService;
import com.escom.utils.Constantes;


public class LoginActivity extends Activity {

    private String nick, pass;
    private CheckBox cBoxLoginOnline;
    private Button loginAccept;
    private Handler handler;
    private SharedPreferences sharedpreferences;
    private boolean success;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sharedpreferences = getSharedPreferences(Constantes.MY_PREFERENCES, Context.MODE_PRIVATE);

        cBoxLoginOnline = (CheckBox) findViewById(R.id.cBoxLoginOnline);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String message = "";
                int codeResponse = msg.getData().getInt("message");
                switch (codeResponse) {
                    case Constantes.EXITO:
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putString(Constantes.NICK_KEY, nick);
                        editor.putString(Constantes.PASS_KEY, pass);
                        editor.commit();
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                        message = "Exito";
                        break;
                    case Constantes.ERROR_TIEMPO:
                        message = "Tiempo agotado";
                        break;
                    //Usuario Not Found
                    case 101:
                        message = "Usuario no encontrado";
                        break;
                }
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        };
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        if (sharedpreferences.contains(Constantes.NICK_KEY) && sharedpreferences.contains(Constantes.PASS_KEY)) {
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
        }
        super.onResume();
    }

    public void executeLogin(View view) {
        nick = ((EditText) findViewById(R.id.eTnick)).getText().toString();
        pass = ((EditText) findViewById(R.id.eTpass)).getText().toString();
        Log.d("Logger ESCOM", "executeLogin");
        Thread tareaLogin = null;
        if (nick != null && pass != null && !nick.equals("") && !pass.equals("")) {
            if (cBoxLoginOnline.isChecked()) {
                MedicoWebService medicoWebService = new MedicoWebService();
                medicoWebService.setHandler(handler);
                medicoWebService.setContext(getApplicationContext());
                medicoWebService.setSharedPreferences(getSharedPreferences(Constantes.MY_PREFERENCES, Context.MODE_PRIVATE));
                medicoWebService.setNick(nick);
                medicoWebService.setPass(pass);
                tareaLogin = new Thread(medicoWebService);
                tareaLogin.start();
            }else {
                MedicoDAO medicoDAO = new MedicoDAO(getApplicationContext());
                medicoDAO.setHandler(handler);
                medicoDAO.setSharedpreferences(getSharedPreferences(Constantes.MY_PREFERENCES, Context.MODE_PRIVATE));
                medicoDAO.existMedicoByNickPass(nick, pass);
            }

        } else {
            Toast.makeText(getApplicationContext(), "Ingrese Usuario/Contraseña", Toast.LENGTH_SHORT).show();
        }
    }

    public void executeLimpiar(View view) {
        ((EditText) findViewById(R.id.eTnick)).setText("");
        ((EditText) findViewById(R.id.eTpass)).setText("");
    }


}
